package Linux五种IO模型.reactor1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * @author coderFu
 * @create 2022/3/6
 * 模拟客户端进行服务端的链接
 */
public class ClientDemo {

    // 创建网络传输对象

    public static void main(String[] args) {

        String hostName = "127.0.0.1";
        int port = 3333;

        try{
            // 链接到目的地
            Socket socket = new Socket(hostName,port);
            System.out.println("链接目的地："+hostName);

            PrintWriter printWriter = new PrintWriter(socket.getOutputStream());

            // 字符流 转为字节流
            BufferedReader in  = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

            String input;
            while ((input = stdin.readLine()) != null){
                printWriter.println(input);
                // 强制将缓冲区中的数据清空
                printWriter.flush();
                if ("exit".equals(input)){
                    break;
                }
                System.out.println("server:"+in.readLine());
            }
            socket.close();
            System.out.println("客户端关闭");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
